#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/2/7 6:51
@desc
"""

__author__ = 'Eva_J'
import os
import multiprocessing  # 引入multiprocessing模块
import time


def func(msg):          # 要放到子进程中运行的代码
    print("subprocess", os.getpid())
    print("subprocess-parent", os.getppid())
    for i in range(3):
        print(msg)
        time.sleep(1)


if __name__ == "__main__":
    # getpid() 查看当前进程的id，getppid()查看当前进程的父id
    print("mainprocess", os.getpid())
    print("mainprocess-parent", os.getppid())
    # 创建一个新进程
    # 将要放在子进程中执行的方法名和参数传给multiprocessing类
    p = multiprocessing.Process(target=func, args=("hello", ))
    # 启动子进程
    p.start()
    # 设置主进程阻塞，直到子进程执行完毕之后再继续运行
    p.join()
    print("have done.")
